<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
|==========================================================
| Code Igniter - by pMachine
|----------------------------------------------------------
| www.codeignitor.com
|----------------------------------------------------------
| Copyright (c) 2006, pMachine, Inc.
|----------------------------------------------------------
| This library is licensed under an open source agreement:
| www.codeignitor.com/docs/license.html
|----------------------------------------------------------
| File: helpers/ip_helper.php
|----------------------------------------------------------
| JAVASCRIPT HELPER
|==========================================================
*/



function jsInclude($jsFileArray)
{
  $includeStr="";
  if (!empty($jsFileArray))
  {
     $includeStr='<script type="text/javascript" src="'.BASEURL.JS_DIR;
     $includeStr .= implode("\" ></script>\n<script type=\"text/javascript\" src=\"".BASEURL.JS_DIR,$jsFileArray);
     $includeStr .= "\" ></script>\n";
  }
  return $includeStr;
}

/**
 * Makes a json string from a 1d associative array 
 * !works only with associative arrays
 * @todo make this function recursive to enable multidimensional arrays.. or replace by a readymade function to build json
 * @param unknown_type $assocArr
 * @return unknown
 */
function buildJson($assocArr)
{
  $retStr="";
  foreach ($assocArr as $key=>$val)
  {
    switch(gettype($val))
    {
        case "array":
            //if its an array ...
            $cleanval=buildJson($val);
            break;
        case "boolean":
          $cleanval = $val ? "true" : "false" ;
          break;
        case "string":
          if (is_numeric($val)) {
            $cleanval = $val;
          } else {
            $cleanval="'".str_replace("\r","",str_replace("\n",'\n',addslashes($val)))."'";
          }
          break;
        default:
          $cleanval = "'$val'";
          break;  
    }
    $retStr .="'$key':$cleanval,";
  }
  return "{".substr($retStr,0,-1)."}";
}
  

function json2($data)
{
    $json = new json;
	return $json->encode($data);
}
  
//Construit une chaine xml à partir d'un tableau
function buildAjaxParams($paramsArray)
{
  $paramsStr="";
  foreach($paramsArray as $key => $value) {
    $paramsStr.="$key=\"$value\" ";
  }
  return $paramsStr;
}


class json {

/*--------------------------------------------------
Encode the variable/array/object
--------------------------------------------------*/

function encode($input) {

$output = $this->get(NULL, $input);

return $output;

}

/*--------------------------------------------------
Get the encoded variable
--------------------------------------------------*/

function get($key, $value, $parent = NULL) {

$type = $this->type($key, $value);

switch ($type) {

case 'object':
$value = '{'.$this->loop($value, $type).'}';
break;
case 'array':
$value = '['.$this->loop($value, $type).']';
break;
case 'number':
$value = $value;
break;
case 'string':
$value = '"'.$this->escape($value).'"';
break;
case 'boolean':
$value = ($value) ? 'true' : 'false';
break;
case 'null':
$value = 'null';
break;

}

if(!is_null($key) && $parent != 'array')
$value = '"'.$key.'":'.$value;

return $value;

}

/*--------------------------------------------------
Check the type of the variable
--------------------------------------------------*/

function type($key, $value) {

if(is_object($value)) {
$type = 'object';
}
elseif(is_array($value)) {
if($this->is_assoc($value))
$type = 'object';
else
$type = 'array';
}
elseif(is_int($value) || is_float($value)) {
$type = 'number';
}
elseif(is_string($value)) {
$type = 'string';
}
elseif(is_bool($value)) {
$type = 'boolean';
}
elseif(is_null($value)) {
$type = 'null';
}
else {
die($key.' is of an unsupported type.');
}

return $type;

}

/*--------------------------------------------------
Loop through the array/object
--------------------------------------------------*/

function loop($input, $type) {

$output = NULL;

foreach($input as $key => $value) {
$output .= $this->get($key, $value, $type).',';
}

$output = trim($output, ',');

return $output;

}

/*--------------------------------------------------
Escape strings
--------------------------------------------------*/

function escape($string) {

$find = array('\\', '"', '/', "\b", "\f", "\n", "\r", "\t", "\u");
$repl = array('\\\\', '\"', '\/', '\b', '\f', '\n', '\r', '\t', '\u');

$string = str_replace($find, $repl, $string);

return $string;

}

/*--------------------------------------------------
Check if the array is associative
--------------------------------------------------*/

function is_assoc($array) {

krsort($array, SORT_STRING);
return !is_numeric(key($array));

}

}




?>
